package cem.agony.ui.controller;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import cem.agony.model.SagStation;

public class RouteBuilderController
{
    private static class RouteLeg
    {
        private StringProperty separatorContant = new SimpleStringProperty( this, "separator", "to" );

        private SagStation fromStation;
        private SagStation toStation;

        private StringProperty fromName;
        private StringProperty toName;

        public RouteLeg( SagStation from, SagStation to )
        {
            this.fromStation = from;
            this.toStation = to;

            this.fromName = new SimpleStringProperty( this, "fromName", from.getName() );
            this.toName = new SimpleStringProperty( this, "toName", to.getName() );
        }

        public SagStation getFromStation()
        {
            return fromStation;
        }

        public SagStation getToStation()
        {
            return toStation;
        }

        public void setFromStation( SagStation fromStation )
        {
            this.fromStation = fromStation;
            this.fromName.set( fromStation.getName() );
        }

        public void setToStation( SagStation toStation )
        {
            this.toStation = toStation;
            this.toName.set( toStation.getName() );
        }

        public StringProperty getFromNameProperty()
        {
            return fromName;
        }

        public StringProperty getToNameProperty()
        {
            return toName;
        }
    }

    @FXML
    private TableView<RouteLeg> routeTable;
    @FXML
    private TableColumn<RouteLeg, String> fromColumn;
    @FXML
    private TableColumn<RouteLeg, String> separatorColumn;
    @FXML
    private TableColumn<RouteLeg, String> toColumn;

    private Stage dialogStage;

    private ObservableList<String> stationsChoiceList = FXCollections.observableArrayList();
    private ObservableList<RouteLeg> routeList = FXCollections.observableArrayList();

    private boolean okayClicked = false;

    @FXML
    private void initialize()
    {
        // TODO validation for whether that road actually exists
        // TODO display distances in columns and total
        // TODO allow reordering (drag and drop?)
        // TODO signify if route must be in order, starting station, etc.

        routeTable.setItems( routeList );

        fromColumn.setCellValueFactory( new PropertyValueFactory<RouteLeg, String>( "fromName" ) );
        separatorColumn.setCellValueFactory( new PropertyValueFactory<RouteLeg, String>( "separator" ) );
        toColumn.setCellValueFactory( new PropertyValueFactory<RouteLeg, String>( "toName" ) );
    }

    public void setDialogStage( Stage dialogStage )
    {
        this.dialogStage = dialogStage;
    }

    @FXML
    private void handleOkay()
    {
        okayClicked = true;
        dialogStage.close();
    }

    @FXML
    private void handleCancel()
    {
        okayClicked = false;
        dialogStage.close();
    }

    public boolean isOkayClicked()
    {
        return okayClicked;
    }
}